﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using SqlSugar;

namespace WPFNote.Repository
{
    public class Note : RepositoryBasic<Models.Note, Models.NoteSch>, IDal.INote
    {
        /// <summary>
        /// 根据ID查询单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override async Task<Models.Note> SelectAsync(int id)
        {
            return await base.Context.Queryable<Models.Note>()
                .Where(c => c.Id == id)
                .OrderBy(st => st.IsTop, OrderByType.Desc)
                .OrderBy(st => st.Id, OrderByType.Desc)
                .Mapper(c => c.Users, c => c.UserId, c => c.Users.Id)
                .Mapper(c => c.Category, c => c.CategoryId, c => c.Category.Id)
                .FirstAsync();
        }

        /// <summary>
        /// 自定义条件单个查询
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public override async Task<Models.Note> SelectAsync(Models.NoteSch ts)
        {
            var (sql, sugarParameters) = Models.Utility.MySqlWhere.Sql(ts);
            return await base.Context.Queryable<Models.Note>()
                .Where(sql, sugarParameters)
                .OrderBy(st => st.IsTop, OrderByType.Desc)
                .OrderBy(st => st.Id, ts.OrderAcs ? OrderByType.Asc : OrderByType.Desc)
                .Mapper(c => c.Users, c => c.UserId, c => c.Users.Id)
                .Mapper(c => c.Category, c => c.CategoryId, c => c.Category.Id)
                .FirstAsync();
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <returns></returns>
        public override async Task<List<Models.Note>> SelectListAsync()
        {
            return await base.Context.Queryable<Models.Note>()
                .OrderBy(st => st.IsTop, OrderByType.Desc)
                .OrderBy(st => st.Id, OrderByType.Desc)
                .Mapper(c => c.Users, c => c.UserId, c => c.Users.Id)
                .Mapper(c => c.Category, c => c.CategoryId, c => c.Category.Id)
                .ToListAsync();
        }
        /// <summary>
        /// 自定义条件查询列表
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public override async Task<List<Models.Note>> SelectListAsync(Models.NoteSch ts)
        {
            var (sql, sugarParameters) = Models.Utility.MySqlWhere.Sql(ts);
            return await base.Context.Queryable<Models.Note>()
                .Where(sql, sugarParameters)
                .OrderBy(st => st.IsTop, OrderByType.Desc)
                .OrderBy(st => st.Id, ts.OrderAcs ? OrderByType.Asc : OrderByType.Desc)
                .Mapper(c => c.Users, c => c.UserId, c => c.Users.Id)
                .Mapper(c => c.Category, c => c.CategoryId, c => c.Category.Id)
                .ToListAsync();
        }


        public override async Task<List<Models.Note>> SelectListAsync(Models.NoteSch ts, int page, int size, RefAsync<int> total)
        {
            var (sql, sugarParameters) = Models.Utility.MySqlWhere.Sql(ts);
            return await base.Context.Queryable<Models.Note>()
                .Where(sql, sugarParameters)
                .OrderBy(st => st.IsTop, OrderByType.Desc)
                .OrderBy(st => st.Id, ts.OrderAcs ? OrderByType.Asc : OrderByType.Desc)
                .Mapper(c => c.Users, c => c.UserId, c => c.Users.Id)
                .Mapper(c => c.Category, c => c.CategoryId, c => c.Category.Id)
                .ToPageListAsync(page, size, total);
        }
    }
}
